import { HYEventStore } from "hy-event-store"
import { throttle } from "underscore"
import { getSongDetail,getLyric } from "../serive/player"
import { parseLyric } from "../utils/pares-lyric"

export const audioContext = wx.createInnerAudioContext()

const playerStore = new HYEventStore({
    state:{
        playSongList:[],
        playSongIndex:0,

        id: 0,
        currentSong:{},
        lyricInfos:[],
        currentLyricText:"",
        currentLyricIndex:0,
        lyricScrollTop:0,

        currentTime:0,
        durationTime:0,
        sliderValue:0,
        isPlaying:true,
        isSliderChanging:false,

        isFristPlay: true,
        currentModeIndex:0
    },
    actions: {
        playMusicWithSongIdAction(ctx,id) {
            // 0.初始化歌曲状态
            this.currentSong = {}
            this.sliderValue = 0
            this.currentTime = 0
            this.durationTime = 0
            this.currentLyricText = ""
            this.currentLyricIndex = 0

            ctx.id = id 

            // 1.根据id获取歌曲
            getSongDetail(id).then(res => {
                ctx.currentSong = res.songs[0],
                ctx.durationTime = res.songs[0].dt
            })
        
            // 2.根据id获取歌词
            getLyric(id).then(res => {
                ctx.lyricInfos = parseLyric(res.lrc.lyric)
            })
            
            audioContext.stop()
            audioContext.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`
            audioContext.autoplay = true
            
            // 监听播放进度
            if (ctx.isFristPlay) {
                ctx.isFristPlay = false
            
                audioContext.onTimeUpdate(() => {
                    // 获取当前时间
                    ctx.currentTime = audioContext.currentTime * 1000

                    // 匹配正确的歌词
                    let index = ctx.lyricInfos.length -1
                    for(let i = 0 ; i < ctx.lyricInfos.length ; i++){
                        const info = ctx.lyricInfos[i]
                        if (info.time > ctx.currentTime) {
                            index = i - 1
                            break;
                        }
                    }
                    if (index === ctx.currentLyricIndex) return
                
                    const currentLyricText = ctx.lyricInfos[index]?.text
                    
                    ctx.currentLyricText = currentLyricText
                    ctx.currentLyricIndex = index
                    ctx.lyricScrollTop = 35 * index
                })
                // 点击滑块时内部会默认暂停所以要再次明确的设置播放
                audioContext.onWaiting(() => {
                    audioContext.pause()
                })
                audioContext.onCanplay(() => {
                    audioContext.play()
                }),
                audioContext.onEnded(() => {
                    // if (ctx.currentModeIndex === 1) return

                    // 自然播放完成触发 播放下一首
                    this.dispatch("playChangeNewSongAction")
                })
            }
        },
        playMusicStateAction(ctx) {
            if (!audioContext.paused) {
                audioContext.pause()
                ctx.isPlaying = false
            } else {
                audioContext.play()
                ctx.isPlaying = true
            }
        },
        playChangeModeAction(ctx) {
            let index = ctx.currentModeIndex
            index = index + 1
            if (index === 3) index = 0
            ctx.currentModeIndex = index
        },
        playChangeNewSongAction(ctx,isNext = true) {
            const length = ctx.playSongList.length
            let index = ctx.playSongIndex

            // 边界判断
            switch(ctx.currentModeIndex) {
                case 0:
                    index = isNext ? index + 1 : index - 1
                    if (index === length) index = 0
                    if (index === -1) index = length -1
                    break;
                case 1:
                    break;
                case 2:
                    index = Math.floor(Math.random() * length)
                    break;
            }
        
            // 根据当前索引获取歌曲
            const newSong = ctx.playSongList[index]
        
            // 记录当前歌曲在播放列表中的索引
            ctx.playSongIndex = index
            
            // 根据歌曲id播放歌曲
            this.dispatch("playMusicWithSongIdAction",newSong.id)
        }
    }
})

export default playerStore